package com.shop.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shop.common.core.domain.Result;
import com.shop.common.core.utils.StringUtils;
import com.shop.common.security.utils.SecurityUtils;
import com.shop.coupon.domain.Coupon;
import com.shop.coupon.domain.vo.CouponHistoryVo;
import com.shop.coupon.mapper.CouponMapper;
import com.shop.coupon.service.CouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService {

    @Autowired
    private RedisTemplate<String ,String > redisTemplate;

    @Autowired
    private CouponMapper couponMapper;

    /**
     * 优惠券列表
     * @return
     */
    @Override
    public Result couponShow(Integer pageNum ,  Integer pageSize) {
        QueryWrapper<Coupon> queryWrapper = new QueryWrapper<>();
        Page<Coupon> couponPage = new Page<>(pageNum, pageSize);
        Page<Coupon> selectPage = baseMapper.selectPage(couponPage, queryWrapper);
        return Result.success(selectPage);
    }

    /**
     * 新增优惠券
     * @param coupon
     * @return
     */
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.SERIALIZABLE)
    @Override
    public Result couponInsert(Coupon coupon) {
        if (StringUtils.isNotEmpty(coupon.getCouponName()) &&
                baseMapper.selectCount(new LambdaQueryWrapper<Coupon>()
                        .eq(Coupon::getCouponName,coupon.getCouponName())) > 0){
            throw new RuntimeException("优惠券名称重复");
        }
        if (StringUtils.isNotEmpty(coupon.getCode()) &&
                baseMapper.selectCount(new LambdaQueryWrapper<Coupon>()
                        .eq(Coupon::getCode , coupon.getCode())) > 0){
            throw new RuntimeException("优惠码存在冲突");
        }
        int insert = baseMapper.insert(coupon);
        return Result.success(insert);
    }

    /**
     * 优惠券的各种修改(金额，优惠券类型，门槛，数量，限领，使用类型，发行数量，发行状态)
     * @param coupon
     * @return
     */
    @Override
    public Result couponUpdate(Coupon coupon) {
        int byId = baseMapper.updateById(coupon);
        redisTemplate.delete("CouponServiceImpl-couponShow");
        return Result.success(byId);
    }

    /**
     * 优惠券删除
     * @param id
     * @return
     */
    @Override
    public Result couponDelete(Long id) {
        Coupon selectById = baseMapper.selectById(id);
        if (selectById.getPublish() == 1){
            return Result.success("优惠券正在发布中，请停止发布重新删除");
        }
        int deleteById = baseMapper.deleteById(id);
        return Result.success(deleteById);
    }

    /**
     * 通过id查询优惠券详情
     * @param id
     * @return
     */
    @Override
    public Result<Coupon> getCoupon(Long id) {
        Coupon byId = baseMapper.selectById(id);
        return Result.success(byId);
    }

    //优惠券发布
    @Override
    public Result couponUpdPublish(Long id) {
        Integer updPublish = couponMapper.couponUpdPublish(id);
        return Result.success(updPublish);
    }

    //优惠券停止发布
    @Override
    public Result couponUpdStop(Long id) {
        Integer updStop = couponMapper.couponUpdStop(id);
        return Result.success(updStop);
    }

    @Override
    public Result getSelectCoupon() {
        List<Coupon> selectCoupon = couponMapper.getSelectCoupon();
        return Result.success(selectCoupon);
    }

    @Override
    public List<CouponHistoryVo> getCouponByUserId() {
        Long userId = SecurityUtils.getUserId();
        return couponMapper.getCouponByUserId(userId);
    }
}
